package com.haikesi.api.organization.controller;

import com.github.pagehelper.PageInfo;
import com.haikesi.api.base.ApiIndex;
import com.haikesi.api.base.Error;
import com.haikesi.api.base.R;
import com.haikesi.api.base.Type;
import com.haikesi.api.base.annotation.Token;
import com.haikesi.api.organization.domain.vo.*;
import com.haikesi.api.organization.service.IOrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName OrganizationController
 * @Author HandsomeBugs
 * @Date 2019/4/30 9:42
 * @Version 1.0
 **/

@Api(description = "机构组织相关接口", tags = ApiIndex.OrganizationController)
@RequestMapping(value = "/api/organization")
@RestController
public class OrganizationController {

    @Autowired
    private IOrganizationService service;



    @ApiOperation(value = "查询列表")
    @GetMapping(value = "/list")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R<PageInfo<List<OrganizationVO>>> list(
            @ApiParam(value = "查询参数") @ModelAttribute OrganizationSearchVO searchVO) {
        List<OrganizationVO> list = service.getList(searchVO);
        PageInfo pageInfo = new PageInfo(list);
        return new R(pageInfo);
    }


    @ApiOperation(value = "查询属性列表")
    @GetMapping(value = "/treeList")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R<List<OrganizationTreeVO>> getTreeList(
            @ApiParam(value = "查询参数") @ModelAttribute OrganizationTreeSearchVO searchVO) {
        List<OrganizationTreeVO> list = service.getTreeList(searchVO);
        return new R(list);
    }


    @ApiOperation(value = "创建")
    @PostMapping(value = "")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R create(
            @ApiParam(value = "添加参数") @Valid @RequestBody OrganizationSaveVO seveVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        return new R().setData(service.create(seveVO));
    }


    @ApiOperation(value = "修改")
    @PutMapping(value = "")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R update(
            @ApiParam(value = "修改参数") @Valid @RequestBody OrganizationUpdateVO updateVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        service.update(updateVO);
        return new R();
    }



    @ApiOperation(value = "删除")
    @DeleteMapping(value = "")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R delete(
            @ApiParam(value = "修改参数") @RequestParam String id) {
        service.delete(id);
        return new R();
    }



}
